package com.github.bitsapling.sapling.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.bitsapling.sapling.entity.User;
import com.github.bitsapling.sapling.result.GraceJSONResult;
import com.github.bitsapling.sapling.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class adminController {
    @Resource
    private UserService userService;

    @PostMapping("/deleteUser")
    @SaCheckPermission(value = "user:invite", orRole = "admin")
    public Map<String, Object> DeleteUser(@RequestBody Map<String, Object> userdata){
        Long userId = Long.parseLong(userdata.get("userId").toString());
        User user = userService.getUserById(userId);
        Map<String, Object> result = new HashMap<>();
        if (user == null) {
            result.put("error","用户不存在" );
            return result;
        }
        if (userService.deleteUser(user)) {
            result.put("success", "用户删除成功");
            return result;
        }
        result.put("error", "删除操作失败");
        return result;
    }

    @GetMapping("/Userlist")
    @SaCheckPermission(value = "user:invite", orRole = "admin")
    public GraceJSONResult DeleteUser(){
        return GraceJSONResult.ok(userService.getAllUsers());
    }


}
